package com.alm.token.ui.viewpagefragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alm.token.ConStant;
import com.alm.token.R;
import com.alm.token.base.BaseAdapter;
import com.alm.token.base.BaseViewPageFragment;
import com.alm.token.common.RefreshLoadMoreManager;
import com.alm.token.ui.activity.BlockWalletLoginActivity;
import com.alm.token.ui.activity.CandyOrderDetailActivity;
import com.alm.token.ui.activity.PayPwdActivity;
import com.alm.token.ui.activity.QuantitativeOrderDetailActivity;
import com.alm.token.ui.data.bean.CandyOrderListData;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.BaseView;
import com.alm.token.utils.CommonUtil;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;

public class CandyListPageFrag extends BaseViewPageFragment implements BaseView<CandyOrderListData> {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.tv_clickreload)
    TextView tvClickreload;
    @BindView(R.id.nodatashow_body)
    RelativeLayout nodatashowBody;
    Unbinder unbinder;
    private RefreshLoadMoreManager<CandyOrderListData.DataBean> manager;

    private List<CandyOrderListData.DataBean> dataBeans = new ArrayList<>();

    public static CandyListPageFrag newInstance() {
        CandyListPageFrag fragment = new CandyListPageFrag();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected MyPreenter initPreenter() {
        return preenter = new MyPreenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.viewpage_fragment_message_base;
    }

    @Override
    protected void initVariables() {

    }

    @Override
    protected void initViews(View view) {
        initManager();
        swipeRefreshLayout.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {

    }

    private void initManager() {
        MessageAdapter adapter = new MessageAdapter(getActivity());
        manager = new RefreshLoadMoreManager.Builder(getActivity())
                .setAdapter(adapter)
                .setRecyclerView(recyclerView)
                .setSwipeRefreshLayout(swipeRefreshLayout)
                .setOnListItemClickListener(new RefreshLoadMoreManager.OnListItemClickListener() {
                    @Override
                    public void onListItemClick(BaseAdapter.BaseItem item, int position) {
                        if (dataBeans != null && dataBeans.size() > position) {
                            Intent intent = new Intent(getContext(), CandyOrderDetailActivity.class);
                            intent.putExtra("id", dataBeans.get(position).getId());
                            startActivity(intent);
                        }

                    }
                })
                .setOnRefreshListener(new RefreshLoadMoreManager.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
//                        showToast("onRefresh");
                        swipeRefreshLayout.setRefreshing(false);
                        preenter.getCandyOrderList();
                    }
                })
                .build();
    }

    @Override
    protected void loadData() {


    }

    @Override
    public void onResume() {
        super.onResume();
        preenter.getCandyOrderList();
    }

    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            getActivity().sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            getActivity().finish();
        } else if (staue == 4) {
//            showToast(getString(R.string.paypasw_hint_setpaypsw));
//            Intent intent = new Intent(getActivity(), PayPwdActivity.class);
//            intent.putExtra("PayPwdActivityType", PayPwdActivity.PAYPSW_SET);
//            startActivity(intent);
//            getActivity().finish();
            showToSetPaypswDialog();
        }else if (staue == 666) {
            showToUpdata();
        }
    }

    @Override
    public void getData(CandyOrderListData candyOrderListData) {

        if (candyOrderListData != null && candyOrderListData.getStatus() == 0) {
            if (candyOrderListData.getData() != null) {
                dataBeans = candyOrderListData.getData();
                for (int i = 0; i < dataBeans.size(); i++) {
                    dataBeans.get(i).setLayoutId(MessageAdapter.LAYOUT_ID);
                }
                manager.setData(dataBeans);
            }
        } else showToast(candyOrderListData.getMsg());

        if (dataBeans.size() == 0) {
            swipeRefreshLayout.setVisibility(View.GONE);
            nodatashowBody.setVisibility(View.VISIBLE);
        } else {
            swipeRefreshLayout.setVisibility(View.VISIBLE);
            nodatashowBody.setVisibility(View.GONE);
        }
    }

    @Override
    public void onError(String error) {

        showToast(getString(R.string.common_loaderrritry));
    }

    @Override
    public void ShowProgress() {

        showWaitDialog();
    }

    @Override
    public void hideProgress() {

        hideWaitDialog();
    }

    @OnClick(R.id.tv_clickreload)
    public void onViewClicked() {
        preenter.getCandyOrderList();
    }


    public class MessageAdapter extends BaseAdapter<CandyOrderListData.DataBean> {

        public static final int LAYOUT_ID = R.layout.item_candylist_base;

        public MessageAdapter(@NonNull Context context) {
            super(context);
        }

        @Override
        protected void convert(BaseViewHolder helper, CandyOrderListData.DataBean item, int itemType) {
            /**
             * moneyname : G12
             * money : 9.0
             * qrcode : http://v.babih.com/api/candy/home?mycode=51
             * shiji : 0.00
             * id : 51
             * time : 1553309883000
             * status : 0
             */

            helper.setText(R.id.tv_name0, getString(R.string.app_hint_redenvelope));
            helper.setText(R.id.tv_staue1, "" + CommonUtil.saveToPoint4WithNo(item.getShiji()) + "/" + CommonUtil.saveToPoint4WithNo(item.getMoney()));
            helper.setText(R.id.tv_staue, item.getMoneyname());
            helper.setText(R.id.tv_phone0, CommonUtil.getDateTimeFromMillisecond(item.getTime()));
//            helper.setText(R.id.date_end, item.getStatus() == 1 ? getString(R.string.invite_hint_candy_finished)
//                    : item.getStatus() == 2 ? getString(R.string.invite_hint_candy_expire,CommonUtil.saveToPoint4WithNo(item.getMoney()-item.getShiji())+item.getMoneyname())
//                    : getString(R.string.invite_hint_candy_unfinished,CommonUtil.saveToPoint4WithNo(item.getMoney()-item.getShiji())+item.getMoneyname()));
//            helper.setText(R.id.date_end, item.getStatus() == 1 ? getString(R.string.invite_hint_candy_finished)
//                    : item.getStatus() == 2 ? getString(R.string.invite_hint_candy_expire, CommonUtil.saveToPoint4WithNo(item.getMoney() - item.getShiji()) + item.getMoneyname())
//                    : item.getStatus() == 3 ? getString(R.string.invite_hint_candy_expire, CommonUtil.saveToPoint4WithNo(item.getMoney() - item.getShiji()) + item.getMoneyname())
//                    : getString(R.string.invite_hint_candy_unfinished, CommonUtil.saveToPoint4WithNo(item.getMoney() - item.getShiji()) + item.getMoneyname()).split(",")[0]);
            helper.setText(R.id.tv_time0, item.getStatus() == 1 ? getString(R.string.invite_hint_candy_finished)
                    : item.getStatus() == 2 ? getString(R.string.invite_hint_candy_expire).split(",")[0] + getString(R.string.invite_hint_candy_expire_zd)
                    : item.getStatus() == 3 ? getString(R.string.invite_hint_candy_expire).split(",")[0] + getString(R.string.invite_hint_candy_expire_sd)
                    : getString(R.string.invite_hint_candy_unfinished, CommonUtil.saveToPoint4WithNo(item.getMoney() - item.getShiji()) + item.getMoneyname()).split(",")[0]);

            helper.setTextColor(R.id.tv_time0, item.getStatus() == 1 ? getResources().getColor(R.color.quanlitative_flag4)
                    : item.getStatus() == 2 ? getResources().getColor(R.color.quanlitative_flag4)
                    : getResources().getColor(R.color.bg_653fac));
            helper.setVisible(R.id.img_arrowright,true);

        }

    }
}
